package com.lhb.test.ireport;

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Chromaticity;

import org.apache.tomcat.jdbc.pool.DataSource;

import com.lhb.test.print.PrintUtil;

import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;
import net.sf.jasperreports.engine.type.OrientationEnum;

public class JasperReport {
	
	private static DataSource dataSource = null;
	static {
		System.setProperty("java.awt.headless", "false");
		dataSource = new DataSource();
		dataSource.setUsername("nontax_db");
		dataSource.setPassword("1");
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://192.168.1.183:3306/nontax_db?useUnicode=true&characterEncoding=utf8&useSSL=false");
	}
	
	public static void main(String[] args) throws Exception {
//		String reportfile ="E:\\Workspaces\\Nontax\\nontax-master\\report\\src\\main\\webapp\\WEB-INF\\jasper\\report1.jasper";
		String reportfile ="E:\\Documents\\iReport\\report2.jasper";
		
		
		PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
		attrs.add(Chromaticity.MONOCHROME);//颜色 单色 彩色
		attrs.add(PrintUtil.getOrientationRequested(0));// 0:纵向，1：横向 
		attrs.add(PrintUtil.getMediaSizeName(4));//纸张大小
		attrs.add(PrintUtil.getReportMediaPrintableArea(4,5,100,5,80));//设定打印区域与非打印区域
		
		System.out.println("3245437655");
		JasperPrint jasperPrint =getJasperPrint(reportfile);
		
		System.out.println("top:"+jasperPrint.getTopMargin()+",botton:"+jasperPrint.getBottomMargin()
				+ ",left:"+jasperPrint.getLeftMargin()+",right:"+jasperPrint.getRightMargin()
				+",height:"+jasperPrint.getPageHeight()+",width:"+jasperPrint.getPageWidth());
//		jasperPrint.setTopMargin(10);
//		jasperPrint.setBottomMargin(10);
//		jasperPrint.setLeftMargin(40);
//		jasperPrint.setRightMargin(50);
//		jasperPrint.setOrientation(OrientationEnum.PORTRAIT);
		
		JRPrintServiceExporter exporter = new JRPrintServiceExporter();
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
//    	exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, null);
    	exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, attrs);
    	exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, true);
    	exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, true);
    	exporter.setParameter(JRExporterParameter.OFFSET_X, 30);
    	exporter.setParameter(JRExporterParameter.OFFSET_Y, 30);
    	try {
			exporter.exportReport();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("打印失败 ",e);
		}
	}
	
	public static JasperPrint getJasperPrint(String reportfile) throws Exception{
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportfile, null,dataSource.getConnection());
        return jasperPrint;
	}
}
